package com.kingbook.core.generator;


import com.kingbook.common.enums.JavaType;
import com.kingbook.core.cache.CacheConfig;
import com.kingbook.core.enums.PrimaryKeyType;
import com.kingbook.core.exception.ParamsException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @Author: Bucky
 * @Date: 2019/1/2
 * @Version 1.0
 */
public class TableBean {

    private String tableName;

    private String objectName;

    private String comment;

    private boolean createRest;

    private boolean createService;

    private boolean createJavaBean;

    private CacheConfig cacheConfig = new CacheConfig();

    private Map<String, PrimaryKey> primaryKeys = Maps.newLinkedHashMap();

    private Map<String, ColumnBean> columns = Maps.newLinkedHashMap();


    public boolean isPK(ColumnBean columnBean){
        return primaryKeys.containsKey(columnBean.column);
    }


    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public boolean isCreateRest() {
        return createRest;
    }

    public void setCreateRest(boolean createRest) {
        this.createRest = createRest;
    }

    public boolean isCreateService() {
        return createService;
    }

    public void setCreateService(boolean createService) {
        this.createService = createService;
    }

    public boolean isCreateJavaBean() {
        return createJavaBean;
    }

    public void setCreateJavaBean(boolean createJavaBean) {
        this.createJavaBean = createJavaBean;
    }

    public CacheConfig getCacheConfig() {
        return cacheConfig;
    }

    public void setCacheConfig(CacheConfig cacheConfig) {
        this.cacheConfig = cacheConfig;
    }

    public Map<String, ColumnBean> getColumns() {
        return columns;
    }

    public void setColumns(Map<String, ColumnBean> columns) {
        this.columns = columns;
    }

    public Map<String, PrimaryKey> getPrimaryKeys() {
        return primaryKeys;
    }

    public void setPrimaryKeys(Map<String, PrimaryKey> primaryKeys) {
        this.primaryKeys = primaryKeys;
    }
}

class ColumnBean {

    String column;
    JavaType javaType;
    String remark;
    boolean nullable;

    ColumnBean(String column, JavaType javaType, String remark, boolean nullable) {
        this.column = column;
        this.javaType = javaType;
        this.remark = remark;
        this.nullable = nullable;
        if ("UNKNOW".equals(this.javaType.name())) {
            throw new ParamsException("未知的字段类型: column=\"" + column + "\" jdbcType=\"" + javaType + "\"");
        }
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }
}


class PrimaryKey {

    String column;
    PrimaryKeyType primaryKeyType;
    String sequenceName;
    String functionName;

    PrimaryKey(String column, String generator, String sequenceName,String functionName) {
        this.column = column;
        this.primaryKeyType = PrimaryKeyType.getInstance(generator);
        this.sequenceName = sequenceName;
        this.functionName = functionName;
    }


    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public PrimaryKeyType getPrimaryKeyType() {
        return primaryKeyType;
    }

    public void setPrimaryKeyType(PrimaryKeyType primaryKeyType) {
        this.primaryKeyType = primaryKeyType;
    }

    public String getSequenceName() {
        return sequenceName;
    }

    public void setSequenceName(String sequenceName) {
        this.sequenceName = sequenceName;
    }

    public String getFunctionName() {
        return functionName;
    }

    public void setFunctionName(String functionName) {
        this.functionName = functionName;
    }
}